package com.zfei.gfb.controller;

import com.google.common.collect.Lists;
import com.zfei.gfb.bean.Const;
import com.zfei.gfb.bean.Status;
import com.zfei.gfb.bean.StatusException;
import com.zfei.gfb.core.Auth;
import com.zfei.gfb.core.HttpJson;
import com.zfei.gfb.core.LogResult;
import com.zfei.gfb.dao.GfbCompanyMapper;
import com.zfei.gfb.dao.GfbJobMapper;
import com.zfei.gfb.model.GfbCompany;
import com.zfei.gfb.model.GfbJob;
import com.zfei.gfb.query.GfbCompanyQuery;
import com.zfei.gfb.request.GfbCompanyAuditRequest;
import com.zfei.gfb.request.GfbGetRequest;
import com.zfei.gfb.request.GfbSetRequest;
import com.zfei.gfb.util.Beans;
import com.zfei.gfb.util.Now;
import com.zfei.gfb.vo.GfbCompanyListVo;
import com.zfei.gfb.vo.GfbCompanyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
/**
 * @version 1.0
 * @author
 */
@Auth(type = {Const.VILLAGE_MANAGER, Const.TOWN_MANAGER})
@Api(tags = "企业账号管理接口")
@RestController
@RequestMapping("/web/company")
public class GfbCompanyController  {

	@Resource
	private GfbCompanyMapper gfbCompanyMapper;

	@Resource
	private GfbJobMapper gfbJobMapper;

	@ApiOperation(value = "企业账号认证列表")
	@ApiResponses({
			@ApiResponse(code = 200, message = "",response = GfbCompanyListVo.class),
	})
	@LogResult
	@HttpJson("/pass/list")
	public Object listPass(@RequestBody GfbCompanyQuery query) {
		query.check().build(Now::millis);
		query.setAudit(Const.COMPANY_AUTH_SUCCESS);

		GfbCompanyListVo result = new GfbCompanyListVo();


		int total = gfbCompanyMapper.total(query);
		result.setTotal(total);
		if (total == 0) {
			result.setData(Lists.newArrayList());
			return result;
		}

		List<GfbCompany> records = gfbCompanyMapper.list(query);
		List<GfbCompanyVo> vos  = Beans.copys(records,GfbCompanyVo.class);
		result.setData(vos);
		return result;

	}

	@ApiOperation(value = "企业账号认证审核列表")
	@ApiResponses({
			@ApiResponse(code = 200, message = "",response = GfbCompanyListVo.class),
	})
	@LogResult
	@HttpJson("/nopass/list")
	public Object listNoPass() {
		GfbCompanyListVo result = new GfbCompanyListVo();

		int total = gfbCompanyMapper.totalNoAudit();
		result.setTotal(total);
		if (total == 0) {
			result.setData(Lists.newArrayList());
			return result;
		}

		List<GfbCompany> records = gfbCompanyMapper.listNoAudit();
		List<GfbCompanyVo> vos  = Beans.copys(records,GfbCompanyVo.class);
		result.setData(vos);
		return result;

	}

	@ApiOperation(value = "企业账号认证-禁用/启用")
	@LogResult
	@HttpJson("/set")
	public Object set(@RequestBody GfbSetRequest request) {
		request.checkSetNull(Const::legalTwoStatus);
		Integer status = request.getStatus();
		Integer id = request.getId();

		GfbCompany record = gfbCompanyMapper.selectByPrimaryKey(id);
		if (record == null) {
			throw new StatusException(Status.NOT_EXIST);
		}
		record.setStatus(status);
		gfbCompanyMapper.updateByPrimaryKeySelective(record);

		if(status == Const.STATUS_ON){
			//todo 上线岗位
			List<GfbJob> gfbJobs = gfbJobMapper.selectByUserIdAndStatus(id, Const.STATUS_OFF);
			if(!gfbJobs.isEmpty()){
				gfbJobMapper.updateByUserIdAndStatus(id, Const.STATUS_ON);
			}
		}
		if(status == Const.STATUS_OFF){
			//todo 下线岗位
			List<GfbJob> gfbJobs = gfbJobMapper.selectByUserIdAndStatus(id, Const.STATUS_ON);
			if(!gfbJobs.isEmpty()){
				gfbJobMapper.updateByUserIdAndStatus(id, Const.STATUS_OFF);
			}
		}
		return null;
	}

	@ApiOperation(value = "企业账号审核")
	@LogResult
	@HttpJson("/audit")
	public Object audit(@RequestBody GfbCompanyAuditRequest request) {
		request.checkUpdateNull();

		GfbCompany record = gfbCompanyMapper.selectByPrimaryKey(request.getId());
		if (record == null) {
			throw new StatusException(Status.NOT_EXIST);
		}

		Beans.copyPropertiesIgnoreNull(request, record);
		if(request.getAudit().equals(Const.COMPANY_AUTH_SUCCESS)){
			record.setPassTime(Now.millis());
		}
		gfbCompanyMapper.updateByPrimaryKeySelective(record);

		return null;
	}

	@ApiOperation(value = "详情")
	@ApiResponses({
			@ApiResponse(code = 200, message = "",response = GfbCompanyVo.class),
	})
	@LogResult
	@HttpJson("/get")
	public Object get(@RequestBody GfbGetRequest request) {
		request.checkGetNull();
		Integer id = request.getId();

		GfbCompany record = gfbCompanyMapper.selectByPrimaryKey(id);

		GfbCompanyVo vo = Beans.copy(record, GfbCompanyVo.class);
		return vo;
	}

	@ApiOperation(value = "已认证详情")
	@ApiResponses({
			@ApiResponse(code = 200, message = "",response = GfbCompanyVo.class),
	})
	@LogResult
	@HttpJson("/pass/get")
	public Object getPass(@RequestBody GfbGetRequest request) {
		request.checkGetNull();
		Integer id = request.getId();

		GfbCompany record = gfbCompanyMapper.selectByPrimaryKey(id);

		int count = gfbJobMapper.selectCountByCompany(id);
		GfbCompanyVo vo = Beans.copy(record, GfbCompanyVo.class);
		vo.setJobNum(count);
		return vo;
	}
}
